--[[
	GridStatusParty
	Adds Grid statuses for party leader, raid leader, raid assistant, and master looter.
	Authors: Byron Shelden (Dashkal), Pastamancer, Phanx
	URL: http://www.wowace.com/addons/grid-status-party/
	This file is covered under the BSD-2.0 license.  See LICENSE.TXT for details
--]]

local _, ns = ...

local L = setmetatable(ns.L, {
	__index = function(t, k)
		t[k] = k
		return k
	end
})

local GridRoster = Grid:GetModule("GridRoster")

local GridStatusParty = Grid:GetModule("GridStatus"):NewModule("GridStatusParty")
GridStatusParty.menuName = L["Party Flags"]

GridStatusParty.defaultDB = {
	debug = false,
	alert_partylead = {
		text = L["PL"],
		enable = true,
		color = { r = 0, g = 0.5, b = 1, a = 1 },
		priority = 25,
		range = false,
		icon = "Interface\\GroupFrame\\UI-Group-LeaderIcon",
	},
	alert_raidlead = {
		text = L["RL"],
		enable = true,
		color = { r = 1, g = 0.5, b = 0, a = 1 },
		priority = 25,
		range = false,
		icon = "Interface\\GroupFrame\\UI-Group-LeaderIcon",
	},
	alert_raidassist = {
		text = L["RA"],
		enable = true,
		color = { r = 1, g = 1, b = 0, a = 1 },
		priority = 23,
		range = false,
		icon = "Interface\\GroupFrame\\UI-Group-AssistantIcon",
	},
	alert_masterloot = {
		text = L["ML"],
		enable = true,
		color = { r = 1, g = 1, b = 1, a = 1 },
		priority = 24,
		range = false,
		icon = "Interface\\GroupFrame\\UI-Group-MasterLooter",
	},
}

local function GetOptionsForStatus(status)
	local status_name = status
	local options = {
		text = {
			name = L["Text"],
			desc = L["Text to display on text indicators"],
			order = 50,
			type = "input", usage = L["<text>"],
			get = function()
				return GridStatusParty.db.profile[status_name].text
			end,
			set = function(_, v)
				GridStatusParty.db.profile[ status_name ].text = v
			end,
		},
	}
	return options
end

function GridStatusParty:OnInitialize()
	self.super.OnInitialize(self)

	self:RegisterStatus("alert_partylead", PARTY_LEADER, GetOptionsForStatus("alert_partylead"))
	self:RegisterStatus("alert_raidlead", RAID_LEADER, GetOptionsForStatus("alert_raidlead"))
	self:RegisterStatus("alert_raidassist", RAID_ASSISTANT, GetOptionsForStatus("alert_raidassist"))
	self:RegisterStatus("alert_masterloot", MASTER_LOOTER, GetOptionsForStatus("alert_masterloot"))
end

function GridStatusParty:OnStatusEnable(status)
	if status == "alert_partylead" or status == "alert_raidlead" or status == "alert_raidassist" or status == "alert_masterloot" then
		self:RegisterEvent("RAID_ROSTER_UPDATE", "Update")
		self:RegisterEvent("PARTY_LEADER_CHANGED", "UpdateParty")
		self:RegisterEvent("PARTY_LOOT_METHOD_CHANGED", "UpdateParty")
		self:Update()
	end
end

function GridStatusParty:OnStatusDisable(status)
	if not self.db.profile.alert_partylead.enable and not self.db.profile.alert_raidlead.enable and not self.db.profile.alert_raidassist.enable and not self.db.profile.alert_masterloot.enable then
		self:UnregisterEvent("RAID_ROSTER_UPDATE")
		self:UnregisterEvent("PARTY_LEADER_CHANGED")
		self:UnregisterEvent("PARTY_LOOT_METHOD_CHANGED")
	end
	if status == "alert_raidlead" or status == "alert_raidassist" then
		self:ClearAll("alert_raidlead")
		self:ClearAll("alert_raidassist")
	end
	if status == "alert_partylead" then
		self:ClearAll("alert_partylead")
	end
	if status == "alert_masterloot" then
		self:ClearAll("alert_masterloot")
	end
end

function GridStatusParty:Reset()
	self.super.Reset(self)
	self:OnDisable()
	self:OnEnable()
end

function GridStatusParty:Update()
	local inParty = GetNumPartyMembers() > 0
	local inRaid = GetNumRaidMembers() > 0

	if inRaid then
		self:ClearStatuses()
		self:UpdateRaid()
	elseif inParty then
		self:ClearStatuses()
		self:UpdateParty()
	else
		self:ClearStatuses()
	end
end

function GridStatusParty:ClearStatuses()
	self:ClearAll("alert_partylead")
	self:ClearAll("alert_raidlead")
	self:ClearAll("alert_raidassist")
	self:ClearAll("alert_masterloot")
end

function GridStatusParty:ClearAll(status)
	for name in self.core:CachedStatusIterator(status) do
		self.core:SendStatusLost(name, status)
	end
end

local party_units = { [0] = "player" }
for i = 1, 4 do
	party_units[i] = "party" .. i
end

function GridStatusParty:UpdateParty()
	local lootmethod, ml_partyID, ml_raidID = GetLootMethod()

	local settings_partylead = self.db.profile.alert_partylead
	local settings_masterloot = self.db.profile.alert_masterloot

	for i = 0, GetNumPartyMembers() do
		local unitid = party_units[i]
		local guid = UnitGUID(unitid)

		if UnitIsPartyLeader(unitid) then
			self.core:SendStatusGained(guid, "alert_partylead",
				settings_partylead.priority,
				settings_partylead.range,
				settings_partylead.color,
				settings_partylead.text,
				nil,
				nil,
				settings_partylead.icon)
		end

		if lootmethod == "master" and i == ml_partyID then
			self.core:SendStatusGained(guid, "alert_masterloot",
				settings_masterloot.priority,
				settings_masterloot.range,
				settings_masterloot.color,
				settings_masterloot.text,
				nil,
				nil,
				settings_masterloot.icon)
		end

	end
end

function GridStatusParty:UpdateRaid()
	local settings_raidlead   = self.db.profile.alert_raidlead
	local settings_raidassist = self.db.profile.alert_raidassist
	local settings_masterloot = self.db.profile.alert_masterloot

	local raidAssist = {}
	local x = 1
	local raidLeader
	local raidMasterLooter
	local raid = GetNumRaidMembers()
	if raid > 0 then
		for i = 1, raid do
			local name, rank, _, _, _, _, _, _, _, _, isML = GetRaidRosterInfo(i)

			if rank == 2 then
				raidLeader = name
			elseif rank == 1 then
				raidAssist[x] = name
				x = x + 1
			end
			if isML then
				raidMasterLooter = name
			end
		end
	end

	for guid in GridRoster:IterateRoster() do
		local name, realm = GridRoster:GetNameByGUID(guid)
		if realm then
			name = name.."-"..realm
		end

		if name == raidLeader then
			self.core:SendStatusGained(guid, "alert_raidlead",
				settings_raidlead.priority,
				settings_raidlead.range,
				settings_raidlead.color,
				settings_raidlead.text,
				nil,
				nil,
				settings_raidlead.icon)
		end

		if name == raidMasterLooter then
			self.core:SendStatusGained(guid, "alert_masterloot",
				settings_masterloot.priority,
				settings_masterloot.range,
				settings_masterloot.color,
				settings_masterloot.text,
				nil,
				nil,
				settings_masterloot.icon)
		end

		for i = 1, #raidAssist do
			if name == raidAssist[i] then
				self.core:SendStatusGained(guid, "alert_raidassist",
					settings_raidassist.priority,
					settings_raidassist.range,
					settings_raidassist.color,
					settings_raidassist.text,
					nil,
					nil,
					settings_raidassist.icon)
			end
		end
	end
end